<!--#include file="common/header.html"-->
<body>
    <div class="container-fluid larry-wrapper">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <!--头部搜索-->
                    <form class="layui-form" action="/education/admin/productSku" style="display: none">
                        <input type="hidden" name="_query.pid" id="queryPid">
                        <div class="layui-inline">
                            <button lay-submit class="layui-btn" lay-filter="search" id="search">查找</button>
                        </div>
                    </form>
                <!--列表-->
                <section class="panel panel-padding">
                    <div class="group-button">
                        <button class="layui-btn layui-btn-small modal-catch"
                                data-params='{"content":".add-subcat", "title":"添加","type":"1", "full":"true", "maxmin":"true"}'>
                            <i class="iconfont">&#xe649;</i> 添加
                        </button>
                        <!--<button class="layui-btn layui-btn-small" onclick="viewDetail();">
                            <i class="iconfont">&#xe653;</i>编辑
                        </button>-->
                        <button class="layui-btn layui-btn-small layui-btn-danger ajax-all" data-name="checkbox" data-params='{"url": "/education/admin/productSku/delete"}'>
                            <i class="iconfont">&#xe626;</i> 删除
                        </button>
                    </div>
                    <div id="list" class="layui-form"></div>

                    <div class="text-right" id="page"></div>
                </section>
            </div>
        </div>
    </div>
    <div class="add-subcat">
        <form class="layui-form layui-form-pane" action="/education/admin/productSku/save" id="form1">
            <input type="hidden" name="a.id">
            <input type="hidden" name="a.product_id" id="pid">

            <div class="layui-form-item">
                <label class="layui-form-label">规格名称</label>
                <div class="layui-input-block">
                    <input class="layui-input" name="a.sku_name" type="text" required jq-verify="required"/>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">市场价(元)</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" name="a.sku_market_price" type="number"
                               autocomplete="false" jq-error="必须为数字">
                    </div>

                    <label class="layui-form-label">预售价(元)</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" name="a.sku_pre_price" type="number" required jq-verify="required"
                               autocomplete="false" jq-error="必须为数字">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">现价(元)</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" name="a.sku_price" type="number" required jq-verify="required"
                               autocomplete="false" jq-error="必须为数字">
                    </div>

                    <label class="layui-form-label">会员价(元)</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" name="a.sku_vip_price" type="number" required jq-verify="required"
                               autocomplete="false" jq-error="必须为数字">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">重量(克)</label>
                    <div class="layui-input-inline">
                        <input type="number" name="a.sku_weight" required jq-verify="required" class="layui-input ">
                    </div>

                    <label class="layui-form-label">库存(个)</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" name="a.sku_stock" type="number" required jq-verify="required"
                               autocomplete="false" jq-error="必须为数字">
                    </div>

                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="a.sku_ord" type="number" required jq-verify="required"
                           autocomplete="false" jq-error="排序必须为数字">
                </div>
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <input type="radio" name="a.sku_status" title="启用" value="1" checked />
                    <input type="radio" name="a.sku_status" title="禁用" value="0" />
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">限购</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="a.sku_a" type="number" value="0" placeholder="多少天（整数）"
                           autocomplete="false" jq-error="必须为数字">
                </div>
                <div class="layui-form-mid layui-word-aux" style="font-size: 15px">天限购</div>
                <div class="layui-input-inline">
                    <input class="layui-input" name="a.sku_b" type="number" value="0" placeholder="多少个（整数）"
                           autocomplete="false" jq-error="必须为数字">
                </div>
                <div class="layui-form-mid layui-word-aux" style="font-size: 15px">个</div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">返利方案</label>
                    <div class="layui-input-inline">
                        <select name="a.sku_rebate_id" id="rebateId">
                            <option/>
                        </select>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea name="a.sku_desc" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">规格包装</label>
                <div class="layui-input-block">
                    <textarea name="a.sku_tips" class="layui-textarea"></textarea>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="width:200px">规格图片</label>
                <div class="layui-input-block">
                    <input type="file" name="file" class="layui-upload-file">
                    <input type="hidden" name="a.sku_image" id="skuImage">
                </div>
                <div class="layui-input-inline"><div class="imgbox" style="width: 800px;"></div></div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" type="button" onclick="submitForm();">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

</body>
<script id="list-tpl" type="text/html"
        data-params='{"url":"/education/admin/productSku","dataName":"productSkuData","pageid":"#page", "method":"post"}'>
    <table id="example" class="layui-table lay-even" data-name="productSkuData" data-tplid="list-tpl">
        <thead>
        <tr>
            <th width="30"><input type="checkbox" id="checkall" data-name="checkbox" lay-filter="check"
                                  lay-skin="primary"></th>
            <th>规格名称</th>
            <th>市场价(元)</th>
            <th>现价(元)</th>
            <th>会员价(元)</th>
            <th>重量(克)</th>
            <th>库存(个)</th>
            <th>状态</th>
            <th>图片</th>
            <th>排序</th>
            <th width="120">操作</th>
        </tr>
        </thead>
        <tbody>
        {{# layui.each(d.list, function(index, item){ }}
        <tr>
            <td><input type="checkbox" name="checkbox" value="{{ item.id}}" lay-skin="primary"></td>
            <td>{{ item.sku_name }}</td>
            <td>￥{{ item.sku_market_price ||''}}</td>
            <td>￥{{ item.sku_price }}</td>
            <td>￥{{ item.sku_vip_price }}</td>
            <td>{{ item.sku_weight }}</td>
            <td>{{ item.sku_stock }}</td>
            <td data-list='{"key":"ids={{ item.id}}","filed":"sku_status","render":"true"}'
                data-params='{"url":"/education/admin/productSku/changeStatus/{{ item.id}}"}'
            class="{{item.sku_status == 1 ? '' : 'bg-red' }}">
                {{item.sku_status == 1 ? '启用' : '禁用' }}
            </td>
            <td>
                <div class="layer-photos-demo">
                    <img layer-src="/education{{ item.sku_image }}"  src="/education{{ item.sku_image }}"
                         onclick="viewImg();"
                         style="height:30px;"  alt="图片">
                </div>
            </td>
            <td class="edit" data-list='{"key":"id={{ item.id}}","filed":"sku_ord","render":"true"}'
                data-params='{"url":"/education/admin/productSku/update/{{item.id}}", "data":"id={{ item.id}}" }'>
                {{ item.sku_ord||'' }}
            </td>
            <td>
                <button class="layui-btn layui-btn-mini" onclick="_editSku('{{item.id}}','{{item.sku_name}}');">编辑</button>
                <button class="layui-btn layui-btn-mini layui-btn-danger ajax"
                        data-list='{"key":"id={{ item.id}}","msg":true,"render":"true","action":"del"}'
                        data-params='{"url": "/education/admin/productSku/delete/{{item.id}}","data":"", "method":"post", "confirm":"true"}'>
                    删除
                </button></td>
            </td>
        </tr>
        {{# }); }}
        </tbody>

    </table>
</script>
<script src="../js/layui/layui.js"></script>
<script type="text/javascript" src="../js/lib/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="../js/lib/rest.js"></script>
<script type="text/javascript" src="../../jsfile/jquery/jquery.form.js"></script>
<!--#include file="common/version.html"-->
<script>
    layui.use('user_list');
    function initData(){
        var $v = '{"url":"/education/admin/productSku?_query.pid='+ getQueryString('ids')
            +'","dataName":"productSkuData","pageid":"#page", "method":"post"}';
        $("#list-tpl").attr('data-params', $v);
        $('#queryPid').val(getQueryString('ids'));

        restPost('/education/admin/rebate', {}, function(rs){
            if(rs.status == 200){
                var arr = ['<option value=" ">----</option>'];
                $.each(rs.data.list, function(i, n){
                    var t = n.normal_rate1 +'|' + n.vip_rate1 + '|' + n.normal_rate2 +'|' +n.vip_rate2;
                    arr.push('<option value="'+ n.id +'">'+ n.name +'(' + t + ')' +'</option>')
                });
                $('#rebateId').html(arr.join(''));
            }
        });
    }

    layui.use(['jquery','upload'], function() {
        var box;
        //上传文件设置
        layui.upload({
            elem: '.layui-upload-file',
            url: '/education/api/image/productSku',
            before: function (input) {
                box = $(input).parent('form').parent('div').parent('.layui-input-block');
                if (box.next('div').length > 0) {
                    box.next('div').html('<div class="imgbox"><p>上传中...</p></div>');
                } else {
                    box.after('<div class="layui-input-block"><div class="imgbox"><p>上传中...</p></div></div>');
                }
            },
            success: function (res) {console.log(res)
                if (res.code == 200) {
                    box.next('div').find('div.imgbox').html('<img src="/education' + res.data[0].url + '" alt="..." class="img-thumbnail">');
                    box.find('input[name="a.sku_image"]').val(res.data[0].url);
                    $('#skuImage').val(res.data[0].url);
                } else {
                    box.next('div').find('p').html('上传失败...')
                }
            }
        });
    });

    function viewImg() {
        layui.use('layer', function(){
            var layer = layui.layer;
            layer.photos({
                photos: '.layer-photos-demo'
                ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });
        });
    }

    function beforeAdd(obj){
        $('.imgbox').children().remove();
    }

    function submitForm() {
        var pid = getQueryString('ids');
        $('#pid').val(pid);
        var url = $('#form1 input[name="a.id"]').val() ? '/education/admin/productSku/update' : '/education/admin/productSku/save';
        restInsert(url, {}, $('#form1'), function (rs) {
            if(rs.status == 200){
//                layer.close(layer.index);
                layer.closeAll()
                layer.msg('操作成功');
                $('#search').trigger('click')
            } else {
                layer.msg(rs.msg)
            }
        })
    }

    function _editSku(ids) {
        var index = layer.open({
            type: 1,
            title:'编辑',
            content: $('.add-subcat')
        });
        layer.full(index);
        $('.imgbox').children().remove();
        restGet('/education/admin/productSku/view/' + ids, null, $('#form1'), function(rs){
            if(rs.code == 200 || rs.status == 200){
                var str =  '<img src="/education{src}" alt="..." class="img-thumbnail" style="width: 200px;">';
                var imgDiv = $('input[name="a.sku_image"]').parent().next().children('.imgbox');
                imgDiv.append(str.replace('{src}', rs.data.sku_image));
                //更新选中框状态
                var t = $('input[name="a.sku_status"]');
                for(var i=0; i<t.length; i++) {
                    if($(t[i]).val() == rs.data.sku_status) {
                        $(t[i]).click();
                    }
                }
                var form = layui.form();
                form.render();
            }
        })
    }

    function afterTableLoad(data) {
        $('.bg-red').parent().css('background-color', '#FFB6C1')//#DCDCDC
    }
</script>

</html>